package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.UB;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商铺
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-17 14:11
 */
public class Shop extends BaseModel<Shop> {
    private static final long serialVersionUID = 1L;

    public static final String market_id = "market_id";
    public static final String merchant_id = "merchant_id";
    public static final String code = "code";
    public static final String summary = "summary";
    public static final String address = "address";
    public static final String phone = "phone";
    public static final String logo = "logo";
    public static final String work_hours = "work_hours";
    public static final String status = "status";
    public static final String recommend = "recommend";
    public static final String seq = "seq";

    public static final Shop dao = new Shop();


    @Override
    public Page<Shop> paginate(int pageNumber, int rows, Map<String, String> criteriaMap) {
        String select = "SELECT s.*, "
        		+ " m.name marketName, u.name userName, u.phone userPhone "// c.name categoryName,
        		+ " ,CONCAT(GROUP_CONCAT(sc.name)) categoryName";
        String exceptSelect = " FROM shop s "
        		+ " LEFT JOIN market m ON s.market_id=m.id "
        		//+ " LEFT JOIN shop_category c ON s.category_id=c.id "
        		+ " LEFT JOIN user u ON s.merchant_id=u.id "
        		
        		+ " LEFT JOIN shop_relate_category src ON s.id=src.shop_id "
        		+ " LEFT JOIN shop_category sc ON src.shop_category_id=sc.id ";
        
        String sqlOrderBy = " GROUP BY s.id ORDER BY s.seq ";
        criteriaMap.put(OB, sqlOrderBy);
        
        return super.paginate(select, exceptSelect, pageNumber, rows, criteriaMap);
    }
    
    /**查询shop对应的社区点*/
    public Page<Shop> paginateCommunity(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT s.name shopName,cs.id, "
        		+ "c.name communityName,c.contacts_name contactsName,c.contacts_phone contactsPhone,c.address,c.status ";

        String exceptSelect = " FROM community_shop cs "
        		+ " LEFT JOIN shop s ON s.id=cs.shop_id "
        		+ " LEFT JOIN community c ON cs.community_id=c.id ";
        
        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }


    public PageData<Shop> paginate(PageParam<Map<String, Object>> param) {
        String select = "SELECT s.id, s.name, s.logo, s.merchant_id, s.summary, u.phone," +
                " (CASE WHEN uc.id IS NULL THEN 0 ELSE 1 END) collected";
        String from = " FROM shop s"
                + " LEFT JOIN user u ON s.merchant_id=u.id"
                + " LEFT JOIN user_collect uc ON s.id=uc.shop_id AND uc.user_id=?";
        String where = " WHERE s.market_id=? AND s.status=1";

        int userId = UB.i(param.getP().get("userId"), -1);
        int marketId = UB.i(param.getP().get(ParamNames.marketId), -1);
        int categoryId = UB.i(param.getP().get(ParamNames.categoryId), -1);
        String shopName = (String)param.getP().get(Shop.name);
        int recommend = UB.i(param.getP().get(Shop.recommend), -1);

        List<Object> params = new ArrayList<>();
        params.add(userId);
        params.add(marketId);

        if(categoryId > 0){
            from += " LEFT JOIN shop_relate_category src ON s.id=src.shop_id";
            where += " AND src.shop_category_id=?";
            params.add(categoryId);
        }

        if(shopName != null && !shopName.isEmpty()){
            where += " AND s.name LIKE ?";
            params.add(shopName.concat("%"));
        }

        if(recommend > 0){
            where += " AND s.recommend=1";
        }

        String sort = " ORDER BY s.seq";
        from += where + sort;

        Page<Shop> page = this.paginate(param.getPageNo(), param.getPageSize(), select, from, params.toArray());
        return PageUtil.to(page);
    }

    public List<Shop> findByMerchantId(int merchantId) {
        return find("select * from shop where merchant_id = ?", merchantId);
    }

    //查出所属市场
  	public Market getMarket(){
  		return Market.dao.findById(getInt("market_id"));
  	}

    //查出所属类型
  	public GoodsCategory getCategory(){
  		return GoodsCategory.dao.findById(getInt("category_id"));
  	}
}
